//
//  FightView.cpp
//  DiceWar
//
//  Created by Xiao  Feng on 24/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "FightingAnimLayer.h"
#include "GameUtil.h"

USING_NS_CC;

namespace  
{
    const CCSize kNumDicesDimension = CCSizeMake(100, 100);
    const CCSize kNumPointsDiension = CCSizeMake(200, 100);
    const int kBackgroundSpriteDepth = 1;
    const int kLabelDepth = 2;
}

bool FightingAnimLayer::init()
{
    if (!CCLayer::init()) return false;
    
    m_pBackgroundSprite = createBackgroundSprite();
    if (!m_pBackgroundSprite) return false;
    hideFightResult();
    
    return true;
}

void FightingAnimLayer::startFightingAnim(cocos2d::CCObject* pTarget,cocos2d::SEL_CallFunc pOnAnimationFinished)
{
    CCDelayTime* delay = CCDelayTime::actionWithDuration(1.5f);
    CCCallFunc* callback = CCCallFunc::actionWithTarget(pTarget, pOnAnimationFinished);
    runAction(CCSequence::actions(delay, callback, NULL));
}

void FightingAnimLayer::startFightingAnim(int attackerNumDices, int attackerNumPoints, int defenderNumDices, int defenderNumPoints, CCObject* pTarget, SEL_CallFunc pOnAnimFinished)
{    
    const CCSize kWinSize = GameUtil::getWindowSize();
    CCDelayTime* delayBeforeDisplay = CCDelayTime::actionWithDuration(0.25f);
    CCCallFunc* displayResult = CCCallFunc::actionWithTarget(this, callfunc_selector(FightingAnimLayer::showFightResult));
    CCDelayTime* displayDelay = CCDelayTime::actionWithDuration(1.5f);
    CCCallFunc* hideResult = CCCallFunc::actionWithTarget(this, callfunc_selector(FightingAnimLayer::hideFightResult));
    CCDelayTime* delayAfterDisplay = cocos2d::CCDelayTime::actionWithDuration(0.25f);
    CCCallFunc* callFuncAction = CCCallFunc::actionWithTarget(pTarget, pOnAnimFinished);
    
    runAction(CCSequence::actions(delayBeforeDisplay, displayResult, displayDelay, hideResult, delayAfterDisplay, callFuncAction, NULL));
}

void FightingAnimLayer::showFightResult()
{
    setIsVisible(true);
}

void FightingAnimLayer::hideFightResult()
{
    setIsVisible(false);
}

cocos2d::CCSprite* FightingAnimLayer::createBackgroundSprite()
{
    CCSprite* pSprite = CCSprite::spriteWithFile("fighting_background.png");
    CCAssert(pSprite, "Fail to create background sprite");
    if (!pSprite) return NULL;
    
    const CCSize kWinSize = GameUtil::getWindowSize();
    const CCRect kSpriteRect = pSprite->getTextureRect();
    pSprite->setScaleX(kWinSize.width * 0.65f / kSpriteRect.size.width);
    pSprite->setScaleY(kWinSize.height * 0.65f / kSpriteRect.size.height);
    
    addChild(pSprite, kBackgroundSpriteDepth);
    this->setPosition(ccp(kWinSize.width * 0.5f, 0.5f * kWinSize.height));
    return pSprite;
}

cocos2d::CCLabelTTF* FightingAnimLayer::createNumDicesLabel(const cocos2d::CCPoint& position)
{
    CCLabelTTF* pLabel = CCLabelTTF::labelWithString("0", kNumDicesDimension, cocos2d::CCTextAlignmentCenter, "Arial", 24);
    CCAssert(pLabel, "Fail to create the Num Dices Label");
    if (!pLabel) return NULL;
    
    pLabel->setPosition(position);
    addChild(pLabel, kLabelDepth);
    return pLabel;
}

cocos2d::CCLabelTTF* FightingAnimLayer::createNumPointsLabel(const cocos2d::CCPoint& position)
{
    CCLabelTTF* pLabel = CCLabelTTF::labelWithString("0", kNumPointsDiension, cocos2d::CCTextAlignmentCenter, "Arial", 32);
    CCAssert(pLabel, "Fail to create the Num Dices Label");
    if (!pLabel) return NULL;
    
    pLabel->setPosition(position);
    addChild(pLabel, kLabelDepth);
    return pLabel;
}
